package org.tea.inventory.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.tea.inventory.db.dao.AlumniOrganizeCustomMapper;
import org.tea.inventory.db.dao.AlumniOrganizeMapper;
import org.tea.inventory.db.dao.AlumniOrganizeUserCustomMapper;
import org.tea.inventory.db.dao.AlumniOrganizeUserMapper;
import org.tea.inventory.db.domain.AlumniOrganize;
import org.tea.inventory.db.domain.AlumniOrganizeCustom;
import org.tea.inventory.db.domain.AlumniOrganizeUser;
import org.tea.inventory.db.domain.AlumniOrganizeUserCustom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AlumniOrganizeService {

    @Autowired
    private AlumniOrganizeMapper alumniOrganizeMapper;

    @Autowired
    private AlumniOrganizeCustomMapper alumniOrganizeCustomMapper;

    @Autowired
    private AlumniOrganizeUserMapper alumniOrganizeUserMapper;

    @Autowired
    private AlumniOrganizeUserCustomMapper alumniOrganizeUserCustomMapper;


    @Transactional
    public int insertSelective(AlumniOrganize record){
        return alumniOrganizeMapper.insertSelective(record);
    }

    public AlumniOrganize selectByPrimaryKey(Integer id){
        return alumniOrganizeMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public int updateByPrimaryKeySelective(AlumniOrganize record){
        return alumniOrganizeMapper.updateByPrimaryKeySelective(record);
    }


    /**
     * 根据type查询
     *
     * @param type organize类型
     * @return
     */
    public Map<String,Object> selectOrganizeByTypeAndName(String type,String orgName, Integer page,Integer limit){

        PageHelper.startPage(page, limit);

        AlumniOrganizeCustom alumniOrganizeCustom = new AlumniOrganizeCustom();
        alumniOrganizeCustom.setOrganizeType(type);
        alumniOrganizeCustom.setOrganizeName(orgName);
        List<AlumniOrganizeCustom> list = alumniOrganizeCustomMapper.selectOrganizeBySelective(alumniOrganizeCustom);

        PageInfo pageinfo = new PageInfo(list);
        Map<String,Object> resMap = new HashMap<String,Object>();
        resMap.put("items",list);
        resMap.put("pageNum",pageinfo.getPageNum());
        resMap.put("pageSize", pageinfo.getPageSize());
        resMap.put("startRow", pageinfo.getStartRow());
        resMap.put("endRow", pageinfo.getEndRow());
        resMap.put("total", pageinfo.getTotal());
        resMap.put("pages", pageinfo.getPages());


        return resMap;
    }

    /**
     * 根据type和name查询专委会
     *
     * @return
     */
    public Map<String,Object> selectOrganizePlace(String organizeType,String organizeName, Integer page,Integer limit){
        PageHelper.startPage(page, limit);

        AlumniOrganizeCustom alumniOrganizeCustom = new AlumniOrganizeCustom();
        if (organizeType != null){
            alumniOrganizeCustom.setOrganizeType(organizeType);
        }

        if (organizeName != null){
            alumniOrganizeCustom.setOrganizeName(organizeName);
        }

        alumniOrganizeCustom.setIsDelete(true);
        List<AlumniOrganizeCustom> list = alumniOrganizeCustomMapper.selectOrganizeBySelective(alumniOrganizeCustom);


        PageInfo pageinfo = new PageInfo(list);
        Map<String,Object> resMap = new HashMap<String,Object>();
        resMap.put("items",list);
        resMap.put("pageNum",pageinfo.getPageNum());
        resMap.put("pageSize", pageinfo.getPageSize());
        resMap.put("startRow", pageinfo.getStartRow());
        resMap.put("endRow", pageinfo.getEndRow());
        resMap.put("total", pageinfo.getTotal());
        resMap.put("pages", pageinfo.getPages());

        return resMap;
    }



    /**
     * 根据userId和orgType查询我的组织
     *
     * @param userId 用户id
     * @param organizeType 组织类型
     * @return
     */
    public List<AlumniOrganizeCustom> selectMyOrganizeByUserIdAndOrgType(Integer userId,String organizeType){
        return alumniOrganizeCustomMapper.selectMyOrganizeByUserIdAndOrgType(userId,organizeType);
    }
    
    /**
     * 根据userId查询我的组织
     *
     * @param userId 用户id
     * @return
     */
    public List<AlumniOrganizeCustom> selectMyOrganizeByUserId(Integer userId){
        return alumniOrganizeCustomMapper.selectMyOrganizeByUserId(userId);
    }

    public int createOrganizeInterest(AlumniOrganize alumniOrganize){

        alumniOrganize.setCreateDate(LocalDateTime.now());
        alumniOrganize.setIsDelete(false);
        alumniOrganize.setOrganizeType("3");
        alumniOrganizeMapper.insertSelective(alumniOrganize);

        AlumniOrganizeUser alumniOrganizeUser = new AlumniOrganizeUser();

        alumniOrganizeUser.setFkOrganizeId(alumniOrganize.getId());
        alumniOrganizeUser.setFkUserId(alumniOrganize.getOrganizeUserId());
        alumniOrganizeUser.setIsNormal("1");
        alumniOrganizeUser.setPriType("1");
        alumniOrganizeUser.setCreateDate(LocalDateTime.now());
        alumniOrganizeUser.setIsDelete(true);
        alumniOrganizeUser.setIsForbidden(false);

        int f = alumniOrganizeUserMapper.insertSelective(alumniOrganizeUser);

        return f;

    }


    /**
     * 设置设置专委会成员为群主
     *
     * @param orgId 组织id
     * @param newAdminUserId alumni_organize_user id
     * @return
     */
    public int updateOrganizeAdminUser(Integer orgId,Integer newAdminUserId){


        AlumniOrganizeUserCustom alumniOrganizeUserCustom = new AlumniOrganizeUserCustom();
        alumniOrganizeUserCustom.setFkOrganizeId(orgId);
        alumniOrganizeUserCustom.setPriType("1");

        List<AlumniOrganizeUserCustom> list = alumniOrganizeUserCustomMapper.selectOrganizeUserBySelective(alumniOrganizeUserCustom);

        Integer f = 0;

        for (AlumniOrganizeUserCustom al : list){

            AlumniOrganizeUser u = new AlumniOrganizeUser();
            u.setId(al.getId());
            u.setPriType("2");
            f = alumniOrganizeUserMapper.updateByPrimaryKeySelective(u);
        }

        AlumniOrganizeUser alumniOrganizeUser = new AlumniOrganizeUser();
        alumniOrganizeUser.setId(newAdminUserId);
        alumniOrganizeUser.setPriType("1");

        alumniOrganizeUserMapper.updateByPrimaryKeySelective(alumniOrganizeUser);

        return f;

    }


    /**
     *
     * 添加用户为群主，有则跟新无则添加
     *
     * @param orgId 组织id
     * @param newAdminUserId 用户id
     * @return
     */
    public int OrganizeAdminUser(Integer orgId,Integer newAdminUserId){

        // 清除所有群主
        AlumniOrganizeUserCustom alumniOrganizeUserCustom = new AlumniOrganizeUserCustom();
        alumniOrganizeUserCustom.setFkOrganizeId(orgId);
        alumniOrganizeUserCustom.setPriType("1");

        List<AlumniOrganizeUserCustom> list = alumniOrganizeUserCustomMapper.selectOrganizeUserBySelective(alumniOrganizeUserCustom);

        Integer f = 0;

        for (AlumniOrganizeUserCustom al : list){

            AlumniOrganizeUser u = new AlumniOrganizeUser();
            u.setId(al.getId());
            u.setPriType("2");
            f = alumniOrganizeUserMapper.updateByPrimaryKeySelective(u);
        }


        alumniOrganizeUserCustom = new AlumniOrganizeUserCustom();
        alumniOrganizeUserCustom.setFkOrganizeId(orgId);
        alumniOrganizeUserCustom.setFkUserId(newAdminUserId);

        // 查询是否有群主，有则更新，无则创建
        List<AlumniOrganizeUserCustom> orgUserList = alumniOrganizeUserCustomMapper.selectOrganizeUserBySelective(alumniOrganizeUserCustom);
        if(orgUserList.size()>0){
            AlumniOrganizeUser u = new AlumniOrganizeUser();
            u.setId(orgUserList.get(0).getId());
            u.setPriType("1");
            u.setIsForbidden(false);
            f = alumniOrganizeUserMapper.updateByPrimaryKeySelective(u);
        } else {
            AlumniOrganizeUser u = new AlumniOrganizeUser();
            u.setFkOrganizeId(orgId);
            u.setFkUserId(newAdminUserId);
            u.setIsNormal("1");
            u.setPriType("1");
            u.setCreateDate(LocalDateTime.now());
            u.setIsForbidden(false);
            f = alumniOrganizeUserMapper.insertSelective(u);
        }
        return f;
    }


    public Map<String,Object> selectOrganizeByUserId(AlumniOrganizeCustom alumniOrganizeCustom,Integer page,Integer limit) {
        PageHelper.startPage(page, limit);
        alumniOrganizeCustom.setIsDelete(true);
        List<AlumniOrganizeCustom> list = alumniOrganizeCustomMapper.selectOrganizeByUserId(alumniOrganizeCustom);
        PageInfo pageinfo = new PageInfo(list);
        Map<String,Object> resMap = new HashMap<String,Object>();
        resMap.put("items",list);
        resMap.put("pageNum",pageinfo.getPageNum());
        resMap.put("pageSize", pageinfo.getPageSize());
        resMap.put("startRow", pageinfo.getStartRow());
        resMap.put("endRow", pageinfo.getEndRow());
        resMap.put("total", pageinfo.getTotal());
        resMap.put("pages", pageinfo.getPages());
        return resMap;
    }
    /**
     * @Author yangjh
     * @Description //删除组织
     * @Date 2018/10/21 22:29
     * @Param
     * @return
     **/
    public void deleteOrgById(Integer orgId){
        AlumniOrganize alumniOrganize = alumniOrganizeMapper.selectByPrimaryKey(orgId);
        if(alumniOrganize != null){
            alumniOrganize.setIsDelete(false);
            alumniOrganize.setIsRemove("1");
            alumniOrganizeMapper.updateByPrimaryKeySelective(alumniOrganize);
        }

        //alumniOrganizeCustomMapper.deleteOrgById(orgId);
    }
    /**
     * @Author yangjh
     * @Description //批量删除评论
     * @Date 2018/10/21 22:46
     * @Param
     * @return
     **/
    public void deleteOrgQuantity(List<String> list) {
        for (String id : list) {
            AlumniOrganize alumniOrganize = alumniOrganizeMapper.selectByPrimaryKey(Integer.valueOf(id));
            if(alumniOrganize != null){
                alumniOrganize.setIsDelete(false);
                alumniOrganize.setIsRemove("1");
                alumniOrganizeMapper.updateByPrimaryKeySelective(alumniOrganize);
            }
            //alumniOrganizeCustomMapper.deleteOrgById(Integer.parseInt(id));
        }
    }

    public int countOrganize(AlumniOrganize organize) {

        return alumniOrganizeMapper.countOrganize(organize);

    }
}
